<?php

require_once("../core/lib/phpmailer/class.phpmailer.php");

/**
 * Description of Mailer
 *
 * @author KoPi
 */
class Mailer extends PHPMailer{
        
	public static $c_name = '__img_mail_container_3456__';
	public static $attachImages = true;

    private $head;
    private $bottom;

    function __construct($port = null) {                       
        $this->Config();
        $this->SetDefaultHtml();
        if(!is_null($port)){
        	$this->Port = $port;
        }
    }

    private function Config()
    {
		
		$this->Username = Config::Get('MAIL_USERNAME');
		$this->Password = Config::Get('MAIL_PASSWORD');
		$this->SMTPAuth = Config::Get('MAIL_SMTPAUTH');
		$this->Mailer = Config::Get('MAIL_MAILER');
		$this->Host = Config::Get('MAIL_HOST');
		$this->Port = Config::Get('MAIL_PORT');
		$this->From = Config::Get('MAIL_FROM');
		$this->FromName = Config::Get('MAIL_FROMNAME');
		$this->CharSet = "utf-8";
		// $this->IsSMTP();
		$this->IsHTML(true);

//        $this->Host='mediaflex.pl';
//		//$this->IsSMTP();
//		$this->SetLanguage('pl', '../core/lib/phpmailer/language/');
//		$this->Mailer='smtp';
//        $this->Username = 'smtp@mediaflex.pl';
//        $this->Password = '123test321';
//        $this->SMTPAuth = true;
//        $this->From= 'smtp@mediaflex.pl';
//		$this->FromName = 'Erocam';
//		$this->ContentType = 'text/html';
//        $this->Ishtml(true);
    }

    private function SetDefaultHtml()
    {
		
    }
    
    public function SendEmail($body,$altbody,$subject,$emails = null)
    {
        $this->Subject = $subject;
		if(is_array($emails))
		{
			foreach ($emails as $index => $value) {
				$this->AddAddress($value);
			}
		}
		else {
			if(!empty($emails)) {
			$this->AddAddress($emails);
			}
		}
		$smarty = new TemplateMaster();

		$smarty->template_dir = Config::Get('PATH_SMARTY_TEMPLATE');
		$smarty->compile_dir = Config::Get('PATH_SMARTY_COMPILE');
		$smarty->cache_dir = Config::Get('PATH_SMARTY_CACHE');
		$smarty->plugins_dir[] = Config::Get('PATH_SMARTY_PLUGINS');
		$smarty->cache_lifetime = 999999999;
		$smarty->caching = Config::Get('SMARTY_CACHING_METHOD');
		$smarty->register_block('dynamic', 'SmartyBlockDynamic', false);
		$smarty->assign('urlStatic', Config::Get('URL_STATIC_CONTENT'));

		$smarty->assign('text', $body);
		$smarty->assign('title',$subject);
		$this->Body = $smarty->fetch('partial/Shared/Mail.tpl');
        //$this->Body = $this->head . $body . $this->bottom;
        $this->AltBody = $altbody;
        
    	if(Mailer::$attachImages) {
			$imgContainer = Registry::Get(Mailer::$c_name);
			if(!empty($imgContainer)) {
				foreach($imgContainer as $img) {
					$this->AddEmbeddedImage(PATH_STATIC_CONTENT . 'image' . DIRECTORY_SEPARATOR . $img['img'], $img['name'], $img['img']);
				}
			}
		}

        if(!$this->Send())
        {          
          return false;
        }
        else
        {
            $this->ClearAttachments();
            $this->ClearAddresses();
            $this->ClearAttachments();
            return true;
        }
    }

    public function GetHead() {
        return $this->head;
    }

    public function SetHead($head) {
        $this->head = $head;
    }

    public function GetBottom() {
        return $this->bottom;
    }

    public function SetBottom($bottom) {
        $this->bottom = $bottom;
    }


}
